package com.d404.server;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;

import com.alibaba.fastjson.JSON;
import com.bbs.server.DownloadInfo;

import im.notify.server.Notify;
import manager.MessageManager;
import manager.PersistentManager;

/**
 * 上传坐标信息
 * Servlet implementation class UploadLocation
 */
public class UploadLocation extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public UploadLocation() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//		response.getWriter().append("Served at: ").append(request.getContextPath());
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");
		
		String data = request.getParameter("data");
		Upload upload = JSON.parseObject(data,Upload.class);
		Download download = new Download();
		
//		SessionFactory sf = PersistentManager.sharemanager().getSessionFactory();
//		Session session = sf.getCurrentSession();
//		Transaction tx = session.beginTransaction();
//		try {
//			
//			
//			
//			tx.commit();
//			download.setStatus(DownloadInfo.SUCCESS);
//		}catch(Exception e)
//		{
//			download.setStatus(DownloadInfo.FAILED);
//			tx.rollback();
//			e.printStackTrace();
//		}finally {
//			
//		}
		
		Notify.LocationResult locationResult = new Notify.LocationResult();
		locationResult.setAccount(upload.getFrom());
		locationResult.setGateId(upload.getGateId());
		locationResult.setLongtitude(upload.getJingdu());
		locationResult.setLatitude(upload.getWeidu());
		
		MessageManager.sendSimpleMessage(upload.getTo(), JSON.toJSONString(locationResult));
		
		download.setStatus(DownloadInfo.SUCCESS);
		System.out.println(JSON.toJSONString(download));
		response.getWriter().append(JSON.toJSONString(download));
		
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}
	
	
	public static class Upload
	{
		String gateId;
		String from;
		String to;
		String jingdu;
		String weidu;
		
		
		public String getGateId() {
			return gateId;
		}
		public void setGateId(String gateId) {
			this.gateId = gateId;
		}
		
		public String getJingdu() {
			return jingdu;
		}
		public void setJingdu(String jingdu) {
			this.jingdu = jingdu;
		}
		public String getWeidu() {
			return weidu;
		}
		public void setWeidu(String weidu) {
			this.weidu = weidu;
		}
		public String getFrom() {
			return from;
		}
		public void setFrom(String from) {
			this.from = from;
		}
		public String getTo() {
			return to;
		}
		public void setTo(String to) {
			this.to = to;
		}
		
		
		
	}

	public static class Download
	{
		int status;

		public int getStatus() {
			return status;
		}

		public void setStatus(int status) {
			this.status = status;
		}
		
	}
}
